package hsb.ruoyi.admin.web.controller.monitor;


import hsb.ruoyi.common.annotation.Log;
import hsb.ruoyi.common.constant.CacheConstants;
import hsb.ruoyi.common.core.cache.LocalCache;
import hsb.ruoyi.common.core.controller.BaseController;
import hsb.ruoyi.common.core.domain.AjaxResult;
import hsb.ruoyi.common.core.domain.model.JwtSubject;
import hsb.ruoyi.common.core.domain.model.LoginUser;
import hsb.ruoyi.common.core.page.TableDataInfo;
import hsb.ruoyi.common.enums.BusinessType;
import hsb.ruoyi.common.utils.StringUtils;
import hsb.ruoyi.system.domain.SysUserOnline;
import hsb.ruoyi.system.service.ISysUserOnlineService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 在线用户监控
 *
 * @author ruoyi
 */
@Path("/monitor/online")
@Produces(MediaType.APPLICATION_JSON)
public class SysUserOnlineController extends BaseController {
    @Autowired
    private ISysUserOnlineService userOnlineService;

    @Autowired
    private LocalCache redisCache;

    @GET
    //@PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @Path("/list")
    public TableDataInfo list(@QueryParam("ipaddr") String ipaddr, @QueryParam("userName") String userName) {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        for (String key : keys) {
            JwtSubject subject = redisCache.getCacheObject(key);
            LoginUser user =(LoginUser) subject.getPrincipal();
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUserName())) {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                }
            } else if (StringUtils.isNotEmpty(ipaddr)) {
                if (StringUtils.equals(ipaddr, user.getIpaddr())) {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                }
            } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
                if (StringUtils.equals(userName, user.getUserName())) {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                }
            } else {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }

    /**
     * 强退用户
     */
    //@PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @Path("/{tokenId}")
    public AjaxResult forceLogout(@PathParam("tokenId") String tokenId) {
        redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        return AjaxResult.success();
    }
}
